/***
* Accenture IDC
* Ashok Kumar      v1.0
* Bhumika Udani    v1.2    included Added to Queue field for insert & update condition check   
***/

trigger SyncMembersWithQueue on Assignment_Groups__c (after insert, after delete ,after update) 
{      
    final String assignGroupType = 'Lead';
    Set<ID> userID = new  Set<ID>();
    final Set <ID> assignGepMember = new  Set <ID>();
    set<ID> AGIds = new set<ID>();
    final set<ID> AGIdsSet = new set<ID>();
    Map <ID,Set<ID>> agQueueAndUserMap= new Map<ID,Set<ID>>();
    Set<ID> reInsertUid  = new Set<ID>();
    Set<ID> addToQueueIds= new Set<ID>();
    final List<Assignment_Group_Name__c> assignGroupList  = null;
    
    if (Trigger.isInsert)
    {
        for(Assignment_Groups__c newAGM :trigger.new)
        {
            AGIds.add(newAGM .Group_Name__c); 
            /** Add the member to queue only if Added to queue is checked**/      
            if(newAGM.Added_to_Queue__c == true )
            {
                 addToQueueIds.add(newAGM.id);
            } 
        }
        assignGroupList  =  [select id from Assignment_Group_Name__c where id in :AGIds and Type__c != 

:assignGroupType  ];
        AGIds.clear();
        for(Assignment_Group_Name__c  obj : assignGroupList  )
        {
            AGIds.add(obj.id);
        }
        for(Assignment_Groups__c memb :trigger.new)
        {   
            // check if user belongs to other than Lead assignment group. 
            if(AGIds.contains(memb.Group_Name__c) )
            {
                assignGepMember.add(memb.id);
            }
        }
        assignGepMember.containsAll(addToQueueIds);
        if(assignGepMember.size()>0)
        {
            IfulfillSyncWithQueue.createQueueMember(assignGepMember);
        }
     }
     
     if(Trigger.isDelete || Trigger.isUpdate)
     {
        for(Assignment_Groups__c memb :trigger.old)
        {
            AGIds.add(memb.Group_Name__c);  
        }
        assignGroupList  =  [select id from Assignment_Group_Name__c where id in :AGIds and Type__c != 

:assignGroupType  ];
        AGIds.clear();
        for(Assignment_Group_Name__c  obj : assignGroupList  )
        {
            AGIds.add(obj.id);
        }
     }
     
      
    if (Trigger.isDelete)
    {
       // Delete the user from standard queue only for ifull project.    
        for(Assignment_Groups__c memb :trigger.old)
        {
            if(AGIds.contains(memb.Group_Name__c) )
            {
                userId.add(memb.User__c);
                AGIdsSet.add(memb.Group_Name__c);  
            }
        } 
        // Check if any user need to be deleted from standard queue       
        if(!AGIdsSet.isEmpty())
        {
            IfulfillSyncWithQueue.deleteQueueMember(userID ,AGIdsSet);
        }
    }
    
    if(Trigger.isUpdate)
    {
        for(Assignment_Groups__c oldAGM :trigger.old)
        {
           Assignment_Groups__c newAGM=  trigger.newMap.get(oldAGM.id);
           if(newAGM.Added_to_Queue__c != oldAGM.Added_to_Queue__c && !(newAGM.Added_to_Queue__c) )
           {
             if(AGIds.contains(oldAGM.Group_Name__c) )
             {
                userId.add(oldAGM.User__c);
                AGIdsSet.add(oldAGM.Group_Name__c);  
              }
           }
           else if(newAGM.Added_to_Queue__c != oldAGM.Added_to_Queue__c && (newAGM.Added_to_Queue__c) )
           {
                if(AGIds.contains(oldAGM.Group_Name__c))
                {
                      reInsertUid.add(oldAGM.id);            
                }
           }              
        }    
        
        if(reInsertUid.size() > 0 )
        {
            IfulfillSyncWithQueue.createQueueMember(reInsertUid);
        }
        if(AGIdsSet.size() > 0)
        {
            IfulfillSyncWithQueue.deleteQueueMember(userID ,AGIdsSet);
        }
    }    
    
    }